import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> list = new ArrayList<>();
        int n1 = s.length();
        int n2 = p.length();
        if(n1 < n2){
            return list;
        }
        int[] hash1 = new int[26];
        int[] hash2 = new int[26];

        for(int i = 0; i < n2; i++){
            hash2[p.charAt(i) - 'a']++;
        }
        int count = 0;
        int left = 0;
        for(int right = 0; right < n1; right++){
            char in = s.charAt(right);
            if(++hash1[in - 'a'] <= hash2[in - 'a']){
                count++;
            }
            if(right - left + 1 > n2){
                char out = s.charAt(left);
                if(hash1[out - 'a']-- <= hash2[out - 'a']){
                    count--;
                }
                left++;
            }
            if(count == n2) {
                list.add(left);
            }
        }
        return list;
    }
}
